Ext.define('Zm.view.model.TreeAttrList',{
    alias:'widget.modeltreeattr',
    extend:'Ext.tree.Panel',
    uses:[
        'Zm.i18n.Lang',
        'Zm.model.Attr',
        'Zm.store.Attr',
        'Ext.ux.TreePicker',
        'Ext.grid.plugin.CellEditing'
    ],
    
    initComponent: function(){
        var me = this,
            cm = Zm.Common,
            lang = Zm.i18n.Lang;
        me.cellEditing = Ext.create('Ext.grid.plugin.CellEditing',{errorSummary:false});
        me.store = Ext.create('Zm.store.Attr');
        me.typeStore = Ext.StoreManager.get('TypeDef');
        me.editors = new Ext.util.HashMap();
        me.boolStore = Ext.create('Ext.data.Store',{
            model:'Zm.model.IdText',
            data:lang.tBoolArr
        });
        Ext.apply(me,{
            multiSelect:true,
            plugins: [me.cellEditing],
            rootVisible:false,
            columns:[
                {xtype:'treecolumn',text:lang.tAttrName,width:140,dataIndex:'attrname',editor:'textfield'},
                {
                    text:lang.tVType,
                    dataIndex:'vtid',
                    renderer:me.rendVType,
                    editor:{
                        xtype:'treepicker',
                        store: me.typeStore,
                        rootVisible:false,
                        displayField:'typename',
                        matchFieldWidth:false,
                        listWidth:300
                    }
                },
                {text:lang.tDefVal,dataIndex:'defval',renderer:me.rendVal,getEditor:me.getCellEditor},
                {text:lang.tMemo,dataIndex:'memo',flex:1,editor:'textfield'}/*,
                {text:lang.tIsVisible,dataIndex:'isvisible',renderer:cm.rendBool,editor:{xtype:'combo',valueField:'id',displayField:'text',editable:false,store:me.boolStore,queryMode:'local'}}*/
            ],
            dockedItems: [{
                xtype:'toolbar',
                dock:'top',
                items:[{
                    text:lang.tAdd,
                    iconCls:'attradd',
                    handler:me.addAttr,
                    scope:me
                },{
                    text:lang.tDel,
                    iconCls:'attrdel',
                    itemId:'attrdel',
                    disabled: true,
                    handler:me.delAttr,
                    scope:me
                }]
            }],
            listeners:{
                edit:me.onEdit,
                selectionchange:me.onSelChange
            }
        });
        me.callParent(arguments);
    },
    rendVType:function(val){
        var store = Ext.StoreManager.get('TypeDef'),
            node = store.getNodeById(val);
        if(node){
            return node.data.typename;
        }
        return val;
    },
    addAttr:function(){
        var me = this,
            node = me.getRootNode(),
            rd = Ext.create('Zm.model.Attr');
        rd.set('isLeaf',true);
        rd.set('iconCls','mattr');
        node.appendChild(rd);
    },
    delAttr:function(){
        var me = this,
            lang = Zm.i18n.Lang,
            sm = me.getSelectionModel(),
            records = sm.getSelection(),
            rd;
        if(records.length>0){
            rd = records[0];
            if(rd.parentNode.parentNode) return;
            Ext.MessageBox.show({
                   title:lang.tDel,
                   msg:lang.tDelAttrTip,
                   buttons: Ext.Msg.YESNO,
                   fn: function(btn){
                       if(btn=='yes'){
                           rd.remove();
                       }
                   },
                   icon: Ext.Msg.QUESTION
               });                        
        }
    },
    
    getCellEditor:function(record){
        var me = this.up('modeltreeattr'),
            vtid = record.get('vtid'),
            typedata = {vtid:vtid,editor:record.get('editor')},
            editor = null;
        editor = me.editors.get(vtid);
        if(editor === undefined){
            editor = Zm.Common.getEditor(typedata,false);
            me.editors.add(vtid,editor);
        }
        return editor;
    },
    beforeDestroy: function() {
        var me = this,editor;
        me.callParent();
        for(editor in me.editors){
            Ext.destroy(editor);
        }
    },
    onSelChange: function(grid, selections,eOpt){
        var me = this,
            btnDel = me.down('#attrdel');
        btnDel.setDisabled(selections.length == 0);
    },
    getNodeData:function(node){
        var me = this,
            i,
            ret = new Array(),
            childnode;
        for(i=0;i<node.childNodes.length;i++){
            childnode = node.childNodes[i];
            ret[i] = {data:childnode.data,children:me.getNodeData(childnode)};
        }
        return ret;
    },
    getValue: function(){
        var me = this,
            rootNode = me.store.getRootNode(),
            ret,
            i,
            n=0;
        me.cellEditing.completeEdit();
        ret = me.getNodeData(rootNode);
        return ret;
    },
    setValue:function(ddid){
        var me = this,
            proxy = me.store.getProxy();
        proxy.extraParams = {ddid:ddid};
        me.store.load();
    },
    rendVal:function(val,meta,record,rowIndex,colIndex,store,view){
        return record.get('valdesc');
    },
    onEdit:function(editor,e,eOpt){
        var me = this,rawValue,
            record = e.record,
            vtid = record.get('vtid'),
            store = me.typeStore;
        if(e.colIdx==2){
            rawValue = me.editors.get(vtid).field.rawValue;
            record.set('valdesc',rawValue);
            if(record.get('editor')=='devfield'){//如果是嵌套的话,需要添加子节点
                if(Ext.isEmpty(record.get('attrname'))) record.set('attrname',rawValue);
                if(e.originalValue == e.value){
                                        
                }else{
                    Ext.Ajax.request({
                        url: g_app + '/DevDef/getAttr',
                        params: {ddid: record.get('defval'),devid:record.get('devid')},
                        scope: me,
                        success:function(response){
                            var obj = Ext.decode(response.responseText,true),
                                sm = me.getSelectionModel(),
                                records = sm.getSelection(),
                                rd,i;
                            if(obj==null) return;
                            records[0].removeAll();
                            for(i=0;i<obj.length;i++){
                                rd = Ext.create('Zm.model.Attr',obj[i]);
                                if(records.length>0){
                                    records[0].appendChild(rd);
                                }
                            }
                        },
                        failure:Zm.Common.onAFail
                    });
                }
            }
        }else if(e.colIdx ==1){
            node = store.getNodeById(vtid);
            if(node){
                record.set('editor',node.data.editor);
            }
        }
        record.commit();
    }
});